<?php
namespace app\upload\controller;

use think\Controller;
use think\Db;
use think\Request;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch(null);
    }
    public function upload()
    {
        //判断文件上传是否出错
        if (!$_FILES || $_FILES["file"]["error"]) {
            $this->error('文件太大！或上传失败');
        } else {
            //控制上传的文件类型，大小
            if (
                $_FILES["file"]["type"] == "application/zip" ||
                $_FILES["file"]["type"] == "application/rar" ||
                $_FILES["file"]["type"] == "application/x-tar" ||
                $_FILES["file"]["type"] == "application/x-gzip" ||
                $_FILES["file"]["type"] == "application/x-bzip2" ||
                $_FILES["file"]["type"] == "application/x-xz" ||
                $_FILES["file"]["type"] == "application/x-lzip" ||
                $_FILES["file"]["type"] == "application/x-7z-compressed"
            ) {

                $filename = "static/file/" . date('Y-m-d H:i:s') . $_FILES["file"]["name"];
                //判断文件是否存在
                if (file_exists($filename)) {
                    $this->error('该文件已存在！');
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
                    $this->success(date('Y-m-d H:i:s') . $_FILES["file"]["name"]);
                }
            } else {
                $this->error('文件类型不正确！');
            }
        }
    }
    public function sond()
    {
        /* 发布信息 */
        $file_name = input('file_name', null);
        $email = input('email', null);
        $email_type = input('email_type', null);
        $info = input('info', null);
        $FileName = deleteDataTime($file_name);
        $html = <<<ETP
        <table class="body-wrap" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">
        <tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
            <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
            <td class="container" width="600" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
                <div class="content" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
                    <table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff">
                        <tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                            <td class="content-wrap aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 20px;" align="center" valign="top">
                                <table width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                    <tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                        <td class="content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                            <h1 class="aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; box-sizing: border-box; font-size: 32px; color: #000; line-height: 1.2em; font-weight: 500; text-align: center; margin: 40px 0 0;" align="center">CloverDownLoad</h1>
                                        </td>
                                    </tr>
                                    <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                        <td class="content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                            <h2 class="aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; box-sizing: border-box; font-size: 24px; color: #000; line-height: 1.2em; font-weight: 400; text-align: center; margin: 40px 0 0;" align="center">欢迎您投递您的投递</h2>
                                        </td>
                                    </tr>
                                    <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                        <td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
                                            <table class="invoice" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; text-align: left; width: 80%; margin: 40px auto;">
                                                <tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                    <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;" valign="top">尊敬的作者：<br style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                    </td>
                                                </tr>
                                                
                                                <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                    <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;" valign="top">
                                                          <p>您投递的 <b>$FileName</b> 已进入后台审核队列，审核完成时将会通知您。<br>
                                                    </td>
                                                </tr>
                                                
                                            </tbody></table>
                                        </td>
                                    </tr>
                                    <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                        <td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
                                            <a href="http://baidu.com" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #348eda; text-decoration: underline; margin: 0;" rel="noopener" target="_blank">CloverDownLoad</a>
                                        </td>
                                    </tr>
                                    <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                        <td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
                                        </td>
                                    </tr>
                                </tbody></table>
                            </td>
                        </tr>
                    </tbody></table>
                    <div class="footer" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
                        <table width="100%" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                            <tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                            </tr>
                        </tbody></table>
                    </div>
                </div>
            </td>
            <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
        </tr>
    </tbody></table>
ETP;
        if (
            $file_name &&
            $email &&
            $email_type &&
            $info
        ) {
            if (
                $email_type == '@QQ.com' ||
                $email_type == '@Gmail.com' ||
                $email_type == '@163.com' ||
                $email_type == '@139.com' ||
                $email_type == '@sina.com' ||
                $email_type == 'undefined'
            ) {
                if ($email_type == 'undefined') {
                    $mail = $email;
                } else {
                    $mail = $email . $email_type;
                }
                if (file_exists('static/file/' . $file_name)) {
                    $D_Mail_Start =  D_Mail('投递成功', $mail, $html, '投递成功');
                    if ($D_Mail_Start == 1) {
                        Db::table('new_efi')
                            ->insert([
                                'name' => $file_name,
                                'email' => $email,
                                'email_type' => $email_type,
                                'info' => $info,
                                'ip' => request()->ip(),
                                'datetime' => date('Y-m-d H:i:s'),
                                'delete' => 0
                            ]);
                        $this->success('录入成功');
                    } else {
                        $this->error('邮件发送错误，请检查');
                    }
                } else {
                    $this->success('发生意外错误 上传的文件名字 未匹配到文件');
                }
            } else {
                error('邮箱地址错误');
                $this->error('邮箱地址错误');
            }
        } else {
            error('缺少必要参数');
            $this->error('缺少必要参数');
        }
    }
    public function image_upload()
    {
        $request = Request();
        $domain = $request->domain();
        $extension = pathinfo($_FILES["imgFile"]["name"])['extension'];
        $filename = "static/images/" . md5(date('Y-m-d H:i:s') . $_FILES["imgFile"]["name"]) . ".$extension";
        //判断文件是否存在
        if (file_exists($filename)) {
            $this->error('该文件已存在！');
            //成功时
            $data['error'] = 1;
            $data['message'] = '该文件已存在！请尝试修改名称';
            return json($data);
        } else {
            move_uploaded_file($_FILES["imgFile"]["tmp_name"], $filename);
            $data['error'] = 0;
            $data['url'] = $domain  . '/' . $filename ;
            return json($data);
        }
    }
}
